package com.qfedu.controller;

import com.qfedu.entity.Cart;
import com.qfedu.entity.User;
import com.qfedu.service.CartService;
import com.qfedu.service.impl.CartServiceImpl;
import com.qfedu.utils.BaseServlet;
import com.qfedu.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/cart")
public class CartServlet extends BaseServlet {
    private CartService cartService = new CartServiceImpl();

    /**
     * 查看购物车
     * @param request
     * @param response
     * @return
     */
    public String show(HttpServletRequest request, HttpServletResponse response){
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        int uid = loginUser.getUid();
        try {
            List<Cart> list = cartService.getCartListByUid(uid);
            request.setAttribute("list",list);
            return Constants.FORWARD + "/cart.jsp";
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        request.setAttribute("msg","服务器发生异常");
        return Constants.FORWARD + "/message.jsp";
    }
    public String update(HttpServletRequest request, HttpServletResponse response){
        int cid = Integer.valueOf(request.getParameter("cid"));
        int cnum  = Integer.valueOf(request.getParameter("cnum"));
        String price = request.getParameter("price");
        BigDecimal bigDecimal = new BigDecimal(price);
        BigDecimal bigDecima2 = new BigDecimal(cnum);
        BigDecimal ccount = bigDecimal.multiply(bigDecima2);
        try {
            cartService.update(cnum,ccount,cid);
            return Constants.FORWARD + "/cart.do?method=show";
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("msg","服务器发生异常");
        return Constants.FORWARD + "/message.jsp";
    }
    /**
     * 根据购物车id 删除一条购物出记录
     * @param request
     * @param response
     * @return
     */
    public String delete(HttpServletRequest request, HttpServletResponse response){
        int cid = Integer.valueOf(request.getParameter("cid"));
        try {
            cartService.delete(cid);
            return Constants.FORWARD + "/cart.do?method=show";
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("msg","服务器发生异常");
        return Constants.FORWARD + "/message.jsp";
    }

    /**
     * 根据用户id 清空购物车
     * @param request
     * @param response
     * @return
     */
    public String clear(HttpServletRequest request, HttpServletResponse response){
        int uid = Integer.valueOf(request.getParameter("uid"));
        try {
            int num = cartService.deleteCart(uid);
            return Constants.FORWARD + "/cart.do?method=show";
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("msg","服务器发生异常");
        return Constants.FORWARD + "/message.jsp";
    }

}
